/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *BaseProcessDocumentInfoController
 * 流程文档管理表新
 *
 * 修改纪录
 * 2021-06-17 版本：1.0 maxiaodong 创建。
 * @version 版本：1.0
 * @author 作者：maxiaodong
 * 创建日期：2021-06-17
 */

package com.topscomm.base.controller;

import com.topscomm.base.BaseSystemConst;
import com.topscomm.base.controller.auto.BaseProcessDocumentInfoControllerAuto;
import com.topscomm.base.pojo.BaseProcessDocumentInfoEntity;
import com.topscomm.base.service.IBaseProcessDocumentGroupService;
import com.topscomm.cbo.model.TreeModel;
import com.topscomm.dsp.api.DspServiceApi;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.ParameterCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: maxiaodong
 * @date: 2021-06-17
 * @description:流程文档管理表新Controller
 * @modify:
 */
@RestController
@RequestMapping("base/processDocumentInfo")
public class BaseProcessDocumentInfoController extends BaseProcessDocumentInfoControllerAuto implements IBaseProcessDocumentInfoController {

	@Autowired
	private DspServiceApi dspServiceApi;

	@Autowired
	private IBaseProcessDocumentGroupService baseProcessDocumentGroupService;

	private static final String BSPQueryOADM = "BSPQueryOADM";

	private static final String MCRQueryOADMVersion = "MCRQueryOADMVersion";

	/**
	 * @author: maxiaodong
	 * @date: 2021-06-17
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_title")) {
			String value = paraMap.get("qry_title").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and title like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_category")) {
			String value = paraMap.get("qry_category").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and category = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_enabled")) {
			String value = paraMap.get("qry_enabled").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and enabled = '{0}' ", value);
		}
		return sql.toString();
	}

	/**
	 * @author: maxiaodong
	 * @date: 2021-06-17
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	@GetMapping(value = "/refList")
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		return super.refList(paraMap);
	}

	/**
	 * @description:文件目录树查询方法
	 * @param paraMap
	 * @return
	 * @author: chengengwei
	 * @date: 2021-06-23
	 * @modify:
	 */
	@GetMapping(value = "/queryFileTreeList")
	public ResponseResult<Object> queryFileTreeList(@RequestParam(required = false) Map<String, Object> paraMap) {
		// 从数据接口获取文档中心公司流程目录树
		String templateid = this.BSPQueryOADM;
		paraMap.put("pageSize", "0");// 返回所有记录
		paraMap.put("pageNum", "1");
		// 获取系统参数中配置的文档中心根目录code
		String code = ParameterCache.getValue(BaseSystemConst.BSPQueryOADMCode, "'2.0','3.0'");
		paraMap.put("code", code);
		Page page = dspServiceApi.query(templateid, paraMap);
		List<Map<String, Object>> dataList = (List<Map<String, Object>>) page.getDataList();
		List<Map<String, Object>> lowerKeyDataList = BaseUtil.lowerKey(dataList);
		List<Map<String, Object>> listNewModule = new ArrayList<>();
		listNewModule.addAll(lowerKeyDataList);
		TreeModel treeModel = new TreeModel();
		List<TreeModel> treeList = treeModel.buildTree(listNewModule, true, "流程架构树");
		Map<String, Object> dataMap = new HashMap<>();
		dataMap.put("dataList", treeList);
		return ResponseResult.ok(dataMap);
	}

	/**
	 * @description:文件查询列表
	 * @param paraMap
	 * @return
	 * @author: chengengwei
	 * @date: 2021-06-23
	 * @modify:
	 */
	@GetMapping(value = "/fileList")
	public ResponseResult<Object> fileList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			// 从数据接口获取文件列表
			String templateid = this.BSPQueryOADM;
			paraMap.put("pageSize", ConvertUtil.convertToString(paraMap.get("pageSize")));
			paraMap.put("pageNum", ConvertUtil.convertToString(paraMap.get("currentPage")));
			// 获取系统参数中配置的文档中心根目录code
			String code = ParameterCache.getValue(BaseSystemConst.BSPQueryOADMCode, "'2.0','3.0'");
			paraMap.put("code", code);
			if (paraMap.containsKey("qry_parentid")) {// 树节点id
				String value = paraMap.get("qry_parentid").toString();
				if (!StringUtil.isEmpty(value))
					paraMap.put("parentid", value);
			}
			paraMap.put("state", "9");// 已发布
			Page page = dspServiceApi.query(templateid, paraMap);
			List<Map<String, Object>> dataList = (List<Map<String, Object>>) page.getDataList();
			List<Map<String, Object>> lowerKeyDataList = BaseUtil.lowerKey(dataList);
			page.setDataList(lowerKeyDataList);
			return ResponseResult.ok(page);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @description:加载最新版本文件数据
	 * @param paraMap
	 * @return
	 * @author: chengengwei
	 * @date: 2021-06-23
	 * @modify:
	 */
	@GetMapping(value = "/loadFile")
	public ResponseResult<Object> loadFile(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			// 从数据接口获取文档中心公司流程最新版本文件
			String templateid = this.MCRQueryOADMVersion;
			paraMap.put("pageSize", "1");
			paraMap.put("pageNum", "1");
			if (paraMap.containsKey("url")) {// 流程文件地址
				String value = paraMap.get("url").toString();
				if (!StringUtil.isEmpty(value))
					paraMap.put("groupid", value);
			}
			Page page = dspServiceApi.query(templateid, paraMap);
			List<Map<String, Object>> dataList = (List<Map<String, Object>>) page.getDataList();
			List<Map<String, Object>> lowerKeyDataList = BaseUtil.lowerKey(dataList);
			Map<String, Object> dataMap = new HashMap<>();
			if (lowerKeyDataList.size() > 0) {
				dataMap = lowerKeyDataList.get(0);
			}
			return ResponseResult.ok(dataMap);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}

	/**
	 * @description:流程文档数
	 * @param paraMap
	 * @return
	 * @author: chengengwei
	 * @date: 2021-07-06
	 * @modify:
	 */
	@GetMapping(value = "/queryProcessDocNum")
	public ResponseResult<Object> queryProcessDocNum(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			Map<String, Object> resultMap = new HashMap<>();
			String category = ConvertUtil.convertToString(paraMap.get(BaseProcessDocumentInfoEntity.FieldCategory));
			StringBufferProxy whereSql = new StringBufferProxy();
			// 判断文档配置组是否有效
			whereSql.appendSingle(" code = '{0}' and enabled = 1", category);
			int groupCount = this.baseProcessDocumentGroupService.getCount(whereSql.toString());
			whereSql.clear();
			int resultCount = 0;
			if (groupCount > 0) {
				whereSql.appendSingle(" {0} = '{1}' and enabled = 1 ", BaseProcessDocumentInfoEntity.FieldCategory,
						category);
				resultCount = this.service.getCount(whereSql.toString());
			}
			resultMap.put("count", resultCount);
			return ResponseResult.ok(resultMap);
		} catch (Exception ex) {
			return handleControllerException(ex);
		}
	}
}

